home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / c / cc02.zip / FILEIO.C < prev    next >
Text File  |  1984-08-19  |  2KB  |  101 lines

  1. /* -- fileio.c    file reading and writing benchmark -- */
  2. /* ---- sequentially writes a 65000 byte file on disk
  3.     generates random long integers
  4.     uses these modulo 65000 to read and write strings of ODDNUM bytes
  5.     with the file handling system of the C package
  6.     the random number generator is set to a specific seed,
  7.     so that all compiles should generate a specific code
  8.    ---- */
  9.  
  10. #include "stdio.h"
  11.  
  12. #define ERROR -1
  13. #define READERR 0
  14. #define BEG 0
  15. #define CURR 1
  16. #define END 2
  17. #define READ 0
  18. #define WRITE 1
  19. #define UPDATE 2
  20. #define OKCLOSE 0
  21. #define FILESIZE 65000L
  22. #define COUNT 500
  23. #define C 13849L
  24. #define A 25173L
  25. #define ODDNUM 23
  26.  
  27. long seed = 7L;
  28. long random(), lseek();
  29.  
  30. main () {
  31.  
  32.     int i;
  33.     long j, pos;
  34.     int fd;
  35.     char buffer[ODDNUM + 1];
  36.  
  37.     if ((fd = creat("test.dat", WRITE)) == ERROR)
  38.         abort("Can't create data file\n");
  39.     else    printf("File open for sequential writing\n");
  40.     for (j = 0; j < FILESIZE; ++j)
  41.         if (write(fd, "x", 1) == ERROR)
  42.             abort("Unexpected EOF in writing data file\n");
  43.     if (close(fd) != OKCLOSE)
  44.         abort("Error closing data file\n");
  45.     else    printf("Normal termination writing data file\n");
  46.     if ((fd = open("test.dat", UPDATE)) == ERROR)
  47.         abort("Can't open data file for random reading and writing\n");
  48.     else    printf("File opened for random reading and writing\n");
  49.     for (i = 0; i < COUNT; ++i) {
  50.         j = random(FILESIZE);
  51.         if (j < 0L)
  52.             j = (-j);
  53.         if (FILESIZE - j < ODDNUM)
  54.             continue;
  55.         if ((pos = lseek(fd, j, BEG)) == -1L)
  56.             abort("Error seeking to random offset\n");
  57.         if (read(fd, buffer, ODDNUM) == READERR)
  58.             abort("Error reading at random offset\n");
  59.         j = random(FILESIZE);
  60.         if (j < 0L)
  61.             j = (-j);
  62.         if (FILESIZE - j < ODDNUM)
  63.             continue;
  64.         if ((pos = lseek(fd, j, BEG)) == -1L)
  65.             abort("Error seeking to random offset\n");
  66.         if (write(fd, buffer, ODDNUM) == READERR)
  67.             abort("Error writing at random offset\n");
  68.     }
  69.     if (close(fd) != OKCLOSE)
  70.         abort("Error closing data file\n");
  71.     else    printf("Normal termination from random reading and writing\n");
  72. }
  73.  
  74. long random(size)
  75.  
  76.     long size;
  77.  
  78.     {
  79.         seed = seed * A + C;
  80.         return(seed % size);
  81.     }
  82.  
  83. abort(message)
  84.  
  85.     char *message;
  86.  
  87.     {
  88.         printf(message);
  89.         exit(ERROR);
  90.     }
  91.  
  92.  
  93.  
  94.  
  95.  
  96.  
  97.  
  98.  
  99.  
  100.  
  101.